package com.jy.wms.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.TimeUtils;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.dao.WmsProvinceRankingReportMapper;
import com.jy.wms.pojo.SalesChannelOrderStatusSummary;
import com.jy.wms.pojo.WmsOrderChannel;
import com.jy.wms.pojo.WmsProvinceRankingReport;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import static com.jy.wms.common.base.BaseController.getJSONObject;
@Service
public class SalesChannelOrderStatusSummaryService extends BaseService<WmsProvinceRankingReportMapper, WmsProvinceRankingReport> {

    /*
       各销售渠道订单状态汇总
    */
    public JSONObject saleschannelorderstatussummary(String ckid, String hzid, Map<String, Long> mapp) {
        JSONObject json = getJSONObject();
        Page<SalesChannelOrderStatusSummary> level=null;
        Page<SalesChannelOrderStatusSummary> Thenumber=null;
        //获取时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (mapp.containsKey("fpsj_start") && mapp.containsKey("fpsj_end")){
            long start = mapp.get("fpsj_start");
            long End= mapp.get("fpsj_end");
            Calendar c = Calendar.getInstance();
            c.setTimeInMillis(start);
            java.util.Date date = c.getTime();
            String start_time=sdf.format(date);
            c.setTimeInMillis(End);
            java.util.Date date2 = c.getTime();
            String End_time=sdf.format(date2);
            int page =  mapp.get("page").intValue();
            int rows =  mapp.get("rows").intValue();

            JSONArray array = null;
            try {
                Thenumber= new Page<>();
                //将一级二级三级查询出来用途
                PageHelper.startPage(page, rows);
                level= (Page<SalesChannelOrderStatusSummary>) dao.Querylevel(ckid, hzid, start_time,End_time);

                for (SalesChannelOrderStatusSummary user : level) {
                    //获取用途
                    user.setPurpose(user.getPurpose());
                    //根据id获取一级二级三级
                    if (!user.getDdqdid().equals("") && !user.getDdqdid().equals("0")) {
                        WmsOrderChannel ddqd = dao.ShippingOrderLevel(ckid, hzid, user.getDdqdid());
                        if ("0".equals(ddqd.getFjd())) {
                            user.setClassA(ddqd.getMc());
                        } else {
                            WmsOrderChannel ddqd2 = dao.ShippingOrderLevel(ckid, hzid, ddqd.getFjd());
                            if ("0".equals(ddqd2.getFjd())) {
                                user.setClassA(ddqd2.getMc());
                                user.setClassB(ddqd.getMc());
                            } else {
                                WmsOrderChannel ddqd3 = dao.ShippingOrderLevel(ckid, hzid, ddqd2.getFjd());
                                if ("0".equals(ddqd3.getFjd())) {
                                    user.setClassA(ddqd3.getMc());
                                    user.setClassB(ddqd2.getMc());
                                    user.setClassC(ddqd.getMc());
                                }
                            }
                        }
                        //全部订单
                        List<SalesChannelOrderStatusSummary> FullOrder = dao.OrderOfAllChannels(ckid, hzid, start_time,End_time, user.getClassA(), user.getClassB(),user.getDdqdid());
                        for (SalesChannelOrderStatusSummary fold : FullOrder) {
                            user.setOrdernumber(fold.getOrdernumber());
                            user.setTotalnumber(fold.getTotalnumber());
                            user.setTotalsales(fold.getTotalsales());
                        }
                        //已发货订单
                        List<SalesChannelOrderStatusSummary> Already = dao.Allchannelteamshavebeenshipped(ckid, hzid, start_time,End_time, user.getClassA(), user.getClassB(),user.getDdqdid());
                        for (SalesChannelOrderStatusSummary alredy : Already) {
                            user.setTotalamount(alredy.getTotalamount());
                            user.setAlready(alredy.getAlready());
                            user.setSupportnumber(alredy.getSupportnumber());
                            user.setBoxes(alredy.getBoxes());
                        }
                        //未发货
                        List<SalesChannelOrderStatusSummary> Unshipped = dao.AllChannelsAreNotShipped(ckid, hzid, start_time,End_time, user.getClassA(), user.getClassB(),user.getDdqdid());
                        for (SalesChannelOrderStatusSummary uspd : Unshipped) {
                            user.setUnconsignment(uspd.getUnconsignment());
                            user.setUnshipped(uspd.getUnshipped());
                            user.setUnconsignmentcount(uspd.getUnconsignmentcount());
                        }
                        //退单
                        List<SalesChannelOrderStatusSummary> Refund = dao.RefundOfVariousChannels(ckid, hzid, start_time,End_time, user.getClassA(), user.getClassB(),user.getDdqdid());
                        for (SalesChannelOrderStatusSummary rf : Refund) {
                            user.setRefund(rf.getRefund());
                            user.setOrderquantity(rf.getOrderquantity());
                            user.setRegression(rf.getRegression());
                        }

                    } else {

                        user.setClassA(user.getClassA());
                        user.setClassB(user.getClassB());
                        user.setClassC(user.getClassC());

                        //全部订单
                        List<SalesChannelOrderStatusSummary> FullOrder = dao.OrderOfAllChannels(ckid, hzid, start_time,End_time, user.getClassA(), user.getClassB(),user.getDdqdid());
                        for (SalesChannelOrderStatusSummary fold : FullOrder) {
                            user.setOrdernumber(fold.getOrdernumber());
                            user.setTotalnumber(fold.getTotalnumber());
                            user.setTotalsales(fold.getTotalsales());
                        }
                        //已发货订单
                        List<SalesChannelOrderStatusSummary> Already = dao.Allchannelteamshavebeenshipped(ckid, hzid, start_time,End_time, user.getClassA(), user.getClassB(),user.getDdqdid());
                        for (SalesChannelOrderStatusSummary alredy : Already) {
                            user.setTotalamount(alredy.getTotalamount());
                            user.setAlready(alredy.getAlready());
                            user.setSupportnumber(alredy.getSupportnumber());
                            user.setBoxes(alredy.getBoxes());
                        }
                        //未发货
                        List<SalesChannelOrderStatusSummary> Unshipped = dao.AllChannelsAreNotShipped(ckid, hzid, start_time,End_time, user.getClassA(), user.getClassB(),user.getDdqdid());
                        for (SalesChannelOrderStatusSummary uspd : Unshipped) {
                            user.setUnconsignment(uspd.getUnconsignment());
                            user.setUnshipped(uspd.getUnshipped());
                            user.setUnconsignmentcount(uspd.getUnconsignmentcount());
                        }
                        //退单
                        List<SalesChannelOrderStatusSummary> Refund = dao.RefundOfVariousChannels(ckid, hzid, start_time,End_time, user.getClassA(), user.getClassB(),user.getDdqdid());
                        for (SalesChannelOrderStatusSummary rf : Refund) {
                            user.setRefund(rf.getRefund());
                            user.setOrderquantity(rf.getOrderquantity());
                            user.setRegression(rf.getRegression());
                        }
                    }
                    Thenumber.add(user);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            array = JSONArray.fromObject(Thenumber);
            json = SetStatus.setSearchType(array.toString(), (int) level .getTotal());
        }
        return json;
    }

    /*
        各销售渠道订单状态汇总报表导出
     */
    public List<SalesChannelOrderStatusSummary> saleschannelorderstatussummary(SalesChannelOrderStatusSummary SalesChannelOrderStatusSummary) {
        //获取用户跟id
        String ckid=SalesChannelOrderStatusSummary.getCkid();
        String hzid=SalesChannelOrderStatusSummary.getHzid();
        //时间
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        if(SalesChannelOrderStatusSummary.getFpsj_start()!=null){
            java.sql.Date date =new java.sql.Date(new Long(SalesChannelOrderStatusSummary.getFpsj_start()));
            SalesChannelOrderStatusSummary.setFpsj_start(sdf.format(date));
        }
        if(SalesChannelOrderStatusSummary.getFpsj_end()!=null){
            java.sql.Date date=new Date(new Long(SalesChannelOrderStatusSummary.getFpsj_end()));
            SalesChannelOrderStatusSummary.setFpsj_end(sdf.format(date));
        }

        String start_time=SalesChannelOrderStatusSummary.getFpsj_start();
        String End_time=SalesChannelOrderStatusSummary.getFpsj_end();

        List<SalesChannelOrderStatusSummary> level=null;
        try {
            level=dao.QuerylevelExport(SalesChannelOrderStatusSummary);

        for (SalesChannelOrderStatusSummary user : level) {

            user.setPurpose(user.getPurpose());

            if(!"".equals(user.getDdqdid())){
                WmsOrderChannel ddqd=dao.ShippingOrderLevel(ckid,hzid,user.getDdqdid());
                if("0".equals(ddqd.getFjd())){
                    user.setClassA(ddqd.getMc());
                }else{
                    WmsOrderChannel ddqd2=dao.ShippingOrderLevel(ckid,hzid,ddqd.getFjd());
                    if("0".equals(ddqd2.getFjd())){
                        user.setClassA(ddqd2.getMc());
                        user.setClassB(ddqd.getMc());
                    }else{
                        WmsOrderChannel ddqd3=dao.ShippingOrderLevel(ckid,hzid,ddqd2.getFjd());
                        if("0".equals(ddqd3.getFjd())){
                            user.setClassA(ddqd3.getMc());
                            user.setClassB(ddqd2.getMc());
                            user.setClassC(ddqd.getMc());
                        }
                    }
                }

                //全部订单
                List<SalesChannelOrderStatusSummary> FullOrder = dao.OrderOfAllChannels(ckid, hzid, start_time,End_time, user.getClassA(), user.getClassB(),user.getDdqdid());
                for (SalesChannelOrderStatusSummary fold : FullOrder) {
                    user.setOrdernumber(fold.getOrdernumber());
                    user.setTotalnumber(fold.getTotalnumber());
                    user.setTotalsales(fold.getTotalsales());
                }
                //已发货订单
                List<SalesChannelOrderStatusSummary> Already = dao.Allchannelteamshavebeenshipped(ckid, hzid, start_time,End_time, user.getClassA(), user.getClassB(),user.getDdqdid());
                for (SalesChannelOrderStatusSummary alredy : Already) {
                    user.setTotalamount(alredy.getTotalamount());
                    user.setAlready(alredy.getAlready());
                    user.setSupportnumber(alredy.getSupportnumber());
                    user.setBoxes(alredy.getBoxes());
                }
                //未发货
                List<SalesChannelOrderStatusSummary> Unshipped = dao.AllChannelsAreNotShipped(ckid, hzid, start_time,End_time, user.getClassA(), user.getClassB(),user.getDdqdid());
                for (SalesChannelOrderStatusSummary uspd : Unshipped) {
                    user.setUnconsignment(uspd.getUnconsignment());
                    user.setUnshipped(uspd.getUnshipped());
                    user.setUnconsignmentcount(uspd.getUnconsignmentcount());
                }
                //退单
                List<SalesChannelOrderStatusSummary> Refund = dao.RefundOfVariousChannels(ckid, hzid, start_time,End_time, user.getClassA(), user.getClassB(),user.getDdqdid());
                for (SalesChannelOrderStatusSummary rf : Refund) {
                    user.setRefund(rf.getRefund());
                    user.setOrderquantity(rf.getOrderquantity());
                    user.setRegression(rf.getRegression());
                }

            }else {

                user.setClassA(user.getClassA());
                user.setClassB(user.getClassB());
                user.setClassC(user.getClassC());

                //全部订单
                List<SalesChannelOrderStatusSummary> FullOrder = dao.OrderOfAllChannels(ckid, hzid, start_time,End_time, user.getClassA(), user.getClassB(),user.getDdqdid());
                for (SalesChannelOrderStatusSummary fold : FullOrder) {
                    user.setOrdernumber(fold.getOrdernumber());
                    user.setTotalnumber(fold.getTotalnumber());
                    user.setTotalsales(fold.getTotalsales());
                }
                //已发货订单
                List<SalesChannelOrderStatusSummary> Already = dao.Allchannelteamshavebeenshipped(ckid, hzid, start_time,End_time, user.getClassA(), user.getClassB(),user.getDdqdid());
                for (SalesChannelOrderStatusSummary alredy : Already) {
                    user.setTotalamount(alredy.getTotalamount());
                    user.setAlready(alredy.getAlready());
                    user.setSupportnumber(alredy.getSupportnumber());
                    user.setBoxes(alredy.getBoxes());
                }
                //未发货
                List<SalesChannelOrderStatusSummary> Unshipped = dao.AllChannelsAreNotShipped(ckid, hzid, start_time,End_time, user.getClassA(), user.getClassB(),user.getDdqdid());
                for (SalesChannelOrderStatusSummary uspd : Unshipped) {
                    user.setUnconsignment(uspd.getUnconsignment());
                    user.setUnshipped(uspd.getUnshipped());
                    user.setUnconsignmentcount(uspd.getUnconsignmentcount());
                }
                //退单
                List<SalesChannelOrderStatusSummary> Refund = dao.RefundOfVariousChannels(ckid, hzid, start_time,End_time, user.getClassA(), user.getClassB(),user.getDdqdid());
                for (SalesChannelOrderStatusSummary rf : Refund) {
                    user.setRefund(rf.getRefund());
                    user.setOrderquantity(rf.getOrderquantity());
                    user.setRegression(rf.getRegression());
                }
            }
        }

            System.out.println(level);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return level;
    }
}
